package com.nanfang51g3.ckz.ui.fragment.withdraw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.ui.money.preview.PreviewMoneyInfoActivity;
import com.nanfang51g3.ckz.ui.money.withdraw.WithdrawContract;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/28.
 */
public class WithdrawAliPayFragment extends Fragment {
    public static WithdrawContract.WithdrawActionsListener mWithdrawActionsListener;

    @Bind(R.id.tv_ali_pay_withdraw_name)
    TextView tvAliPayWithdrawName;
    @Bind(R.id.tv_ali_pay_withdraw_id)
    TextView tvAliPayWithdrawId;
    @Bind(R.id.cet_ali_pay_withdraw_input)
    ClearEditText cetAliPayWithdrawInput;
    String ali_pay;
    String  deal_total;


    public static WithdrawAliPayFragment newInstance(Context context, WithdrawContract.WithdrawActionsListener withdrawActionsListener) {
        WithdrawAliPayFragment withdrawAliPayFragment = new WithdrawAliPayFragment();
        Bundle args = new Bundle();
        String ali_pay_ = (String) SPUtil.get(context, "ali_pay_card", "暂未填写");
        String deal_total = (String) SPUtil.get(context, "deal_total", 0.0);
        args.putString("deal_total", deal_total);
        KLog.i("Evd", "ali_pay_card=" + ali_pay_+"-"+deal_total);
        args.putString("ali_pay_card", ali_pay_);

        withdrawAliPayFragment.setArguments(args);
        mWithdrawActionsListener = withdrawActionsListener;
        KLog.i("Evd", mWithdrawActionsListener == null ? "mWithdrawActionsListener - 1" : "mWithdrawActionsListener - 0");
        return withdrawAliPayFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.withdraw_ali_pay_fragment, null);
        ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        ali_pay = bundle.getString("ali_pay_card");
        deal_total = bundle.getString("deal_total");
        KLog.i("Evd", "alipay_=" + ali_pay+"-"+deal_total);
        tvAliPayWithdrawId.setText(ali_pay);
        cetAliPayWithdrawInput.setHint("可提现金额"+deal_total+"元");
        cetAliPayWithdrawInput.setHighlightColor(getResources().getColor(R.color.gray));
        return view;
    }

    @OnClick(R.id.relative_withdraw_ali_pay)
    public void relative_withdraw_ali_pay(View view) {
        startActivity(new Intent(getActivity(), PreviewMoneyInfoActivity.class));
    }
    @Override
    public void onResume() {
        super.onResume();
        KLog.i("Evd","onResume");
          ali_pay = (String) SPUtil.get(getActivity(), "ali_pay_card", "暂未填写");
          deal_total = (String) SPUtil.get(getActivity(), "deal_total", "");
        tvAliPayWithdrawId.setText(ali_pay);
        cetAliPayWithdrawInput.setHint("可提现金额"+deal_total+"元");
        cetAliPayWithdrawInput.setHighlightColor(getResources().getColor(R.color.gray));
    }

    @OnClick(R.id.tv_ali_pay_confirm_withdraw)
    public void tv_ali_pay_confirm_withdraw(View view) {
        if (ali_pay.equals("暂未填写")) {
            EasyToast.showShort(getActivity(), "请填写支付宝信息");
            startActivity(new Intent(getActivity(), PreviewMoneyInfoActivity.class));
            return;
        } else {
            String total = cetAliPayWithdrawInput.getText().toString().trim();
            /**
             * @see  com.nanfang51g3.ckz.ui.money.withdraw.WithdrawPresenter#deal(int, String)
             */
            mWithdrawActionsListener.deal(1, total);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
